<?php
declare (strict_types = 1);

namespace app\common\exception;

use think\facade\Env;

/**
 * 权限错误
 */
class UnauthorizedException extends \RuntimeException
{
    protected $error;
    protected $error_code;

    public function __construct($error = null, $error_code = 104011)
    {
        $error = is_null($error) ? '登录已失效' : $error;
        if ($error_code != 402) {
            $error = Env::get('app_debug') ? $error : '未授权访问';
        }
        $this->error      = $error;
        $this->error_code = $error_code;
        $this->message    = is_array($error) ? implode(PHP_EOL, $error) : $error;
    }

    /**
     * 获取验证错误信息
     * @access public
     * @return array|string
     */
    public function getError()
    {
        return $this->error;
    }

    public function getErrorCode()
    {
        return intval($this->error_code);
    }
}
